﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassModel;
using ClassDAL;
using ClassCommon;

namespace ClassBLL
{
    /// <summary>
    /// 班级排班
    /// </summary>
    public class t_class_schedule_BLL
    {
        /// <summary>
        /// 班级排课数据插入
        /// </summary>
        public static int InsertScheduleClasses(Dictionary<string, List<SchedulingClassModel>> dicts, int fileid)
        {
            if (dicts == null) throw new ArgumentException("dicts is null");

            return t_class_schedule_DAL.InsertScheduleClasses(dicts, fileid);
        }



        /// <summary>
        /// 获取每个班级的老师和课程信息
        /// </summary>
        /// <param name="classid">班级id</param>
        /// <param name="fileid">excel对应的fileid</param>
        public static List<t_class_Schedule_Bind> GetTeacherAndCourseInfo(int classid, int fileid)
        {
            return t_class_schedule_DAL.GetTeacherAndCourseInfo(classid, fileid);
        }


        /// <summary>
        /// 根据课程id获取老师和班级信息
        /// </summary>
        /// <param name="courseid"></param>
        /// <param name="fileid"></param>
        public static List<t_class_Schedule_Bind> GetTeacherAndClassInfo(int courseid, int fileid)
        {
            return t_class_schedule_DAL.GetTeacherAndClassInfo(courseid, fileid);
        }


        /// <summary>
        /// 根据老师id获取老师一周上课的课程节次数
        /// </summary>
        /// <param name="teacherId"></param>
        /// <returns>-1错误！！</returns>
        public static int GetTeacherClassesCount(int teacherId)
        {
            string teacherCondition = t_class_schedule_DAL.GetTeacherClassesCount(teacherId);
            if (string.IsNullOrEmpty(teacherCondition)) return -1;

            if (!teacherCondition.Contains("+")) return Convert.ToInt32(teacherCondition);

            int classesCount = 0;
            teacherCondition.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(item =>
            {
                classesCount += Convert.ToInt32(item);
            });
            classesCount += 1;

            return classesCount;

        }


        /// <summary>
        /// 根据fileid获取班级，根据班级在获取这个班一周要上几节课
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public static int GetClassLessionCountInAWeek(int fileid)
        {
            var list = t_class_schedule_DAL.GetClassLessionCountInAWeek(fileid);

            if (list == null) return -1;

            int lessionCountInAWeek = 0;

            list.ForEach(condition =>
            {
                if (!condition.Contains("+"))
                {
                    lessionCountInAWeek += Convert.ToInt32(condition);
                }
                else
                {
                    condition.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(c =>
                    {
                        lessionCountInAWeek += Convert.ToInt32(c);
                    });

                    lessionCountInAWeek += 1;
                }

            });

            return lessionCountInAWeek;
        }

    }
}
